package io.noties.markwon.base.image.size

import android.graphics.Rect
import io.noties.markwon.base.image.ImageSize

class ImageAutoSizeShowResolverDef : ImageSizeResolverDef() {
    override fun resolveImageSize(
        imageSize: ImageSize?,
        imageBounds: Rect,
        canvasWidth: Int,
        textSize: Float
    ): Rect {
        val rect: Rect
        val w = imageBounds.width()
        rect = if (w > canvasWidth || w * 1f / canvasWidth > 0.5) {
            val reduceRatio = w.toFloat() / canvasWidth
            Rect(
                0,
                0,
                canvasWidth, (imageBounds.height() / reduceRatio + .5f).toInt()
            )
        } else {
            imageBounds
        }
        return rect
    }
}